package com.ynet.middleground.approve.model;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Throwables;
import com.ynet.core.log.IfpLogger;
import com.ynet.host.HostConstants;
import com.ynet.middleground.approve.external.mobile.MobileApproveConfig;
import com.ynet.middleground.approve.utils.ApiUtil;

/**
 * MobileApproveModel 移动审批服务
 *
 * @author suremotoo
 * @date 2023/04/03 15:19
 */
@Component
public class MobileApproveModel {

    @Autowired
    private ApiUtil apiUtil;

    @Autowired
    private MobileApproveConfig mobileApproveConfig;

    /**
     * 推送钉钉消息服务
     *
     * @param processInstanceId 流程实例 id （必填）
     * @param taskId 当前任务 id （必填）
     * @param userId 操作人员（必填）
     */
    public void sendDingtalkNotification(String processInstanceId, String taskId, String userId) {
        Map<String, String> params = new HashMap<>();
        params.put("processInstanceId", processInstanceId);
        params.put("taskId", taskId);
        params.put("userId", userId);

        JSONObject apiResult = null;
        try {
            apiResult =
                apiUtil.sendApi(null, params, mobileApproveConfig.getHost() + mobileApproveConfig.getDingtalkMsgUrl(),
                    HostConstants.HTTP_METHOD_POST, "调用移动审批推送钉钉消息服务");
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "审批中心调用移动审批推送钉钉消息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
        }

        if (apiResult == null) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(),
                "审批中心调用移动审批推送钉钉消息服务异常，未获取到返回值，apiResult: {}", apiResult);
        }
    }

}
